import os
from Factors_Systematic.General import *


# PMI
def Download_PMI(database, datetime1, datetime2):
    #
    df_pmi = EDB('M0017126', datetime1, datetime2, 'PMI', dateAsIndex=False)  # PMI
    Fill_ReleaseDate(df_pmi, lag_release_month=1, release_day=1)
    Save_Systmetic_Factor_To_Database(database, df_pmi, save_name='PMI')
    #
    df_pmi_produce = EDB('M0017127', datetime1, datetime2, 'PMI_Produce', dateAsIndex=False)  # PMI:生产
    Fill_ReleaseDate(df_pmi_produce, lag_release_month=1, release_day=1)
    Save_Systmetic_Factor_To_Database(database, df_pmi_produce, save_name='PMI_Produce')
    #
    df_pmi_newOrder = EDB('M0017128', datetime1, datetime2, 'PMI_NewOrder', dateAsIndex=False)  # PMI:新订单
    Fill_ReleaseDate(df_pmi_newOrder, lag_release_month=1, release_day=1)
    Save_Systmetic_Factor_To_Database(database, df_pmi_newOrder, save_name='PMI_NewOrder')
    #
    df_caixin_pmi = EDB('M0000138', datetime1, datetime2, 'CaiXin_PMI', dateAsIndex=False)  # 财新中国PMI
    Fill_ReleaseDate(df_caixin_pmi, lag_release_month=1, release_day=1)
    Save_Systmetic_Factor_To_Database(database, df_caixin_pmi, save_name='CaiXin_PMI')


# 融合至 Calc_PriceLevel
def Download_PriceLevel(database, datetime1, datetime2):
    #
    # df_cpi = EDB('M0000612', datetime1, datetime2, 'CPI_YoY', dateAsIndex=False)
    # Fill_ReleaseDate(df_cpi, lag_release_month=1, release_day=10)
    # # 储存原始数据
    # Save_Systmetic_Raw_To_Database(database, df_cpi, savedName="M0000612", fieldName="CPI_YoY")
    # # 储存为因子数据
    # Save_Systmetic_Factor_To_Database(database, df_cpi, savedName='CPI_YoY')
    pass


# PMI
def Download_Economics(database, datetime1, datetime2):
    #
    df = EDB('M0000545', datetime1, datetime2, 'Industrial_Value_Added_YoY', dateAsIndex=False)  # 工业增加值:当月同比
    Fill_ReleaseDate(df, lag_release_month=1, release_day=15)
    Save_Systmetic_Factor_To_Database(database, df, save_name='Industrial_Value_Added_YoY')


def Download_Risk_Free_Rate(database, datetime1, datetime2):
    #
    status, df_rf = w.wsd("TB1Y.WI", "close", datetime1, datetime2, usedf=True)
    Save_Systmetic_Factor_To_Database(database, df_rf, save_name='TB1Y', field_name="CLOSE")


def Automatic_Download_Macro_Data(database, datetime2, startEntry=0):
    #
    if datetime2 == None:
        datetime2 = datetime.datetime.today()
    #
    default_datetime1 = datetime2 - datetime.timedelta(days=180)
    #
    # Download_PMI(database, datetime1, datetime2)
    # Download_Economics(database, datetime1, datetime2)
    # Download_PriceLevel(database, datetime1, datetime2)

    last_date = Find_Last_Update_Date(database, "TB1Y")
    Download_Risk_Free_Rate(database, last_date, datetime2)


if __name__ == '__main__':
    #
    pathfilename = os.getcwd() + "\..\Config\config2.json"
    config = Config.Config(pathfilename)
    database = config.DataBase("JDMySQL")
    realtime = config.RealTime()
    #
    datetime1 = datetime.datetime(2000, 1, 1)
    datetime2 = datetime.datetime(2020, 12, 30)
    #
    w.start()
    #
    Automatic_Download_Macro_Data(database, datetime2, startEntry=0)
    #
    # Download_PMI(database, datetime1, datetime2)
    # Download_PriceLevel(database, datetime1, datetime2)
    # Download_Economics(database, datetime1, datetime2)
    # Download_Risk_Free_Rate(database, datetime1, datetime2)